﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.Stock;
using ProTrack.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 其他出库服务接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class OtherExWareHouseController : ControllerBase
    {
        public OtherExWareHouseController(IOtherExWareHouseServices services, LogHelper LogHelper)
        {
            Services = services;
            this.LogHelper = LogHelper;

        }
        public IOtherExWareHouseServices Services { get; }
        public LogHelper LogHelper { get; }

        /// <summary>
        /// 获取所以其他出库
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list =await Services.GetAllAsync();
                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他出库-获取所有", ex, HttpContext);

            }
            return Ok(status);

        }
        /// <summary>
        /// 分页获取其他出库
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PasgeSize"></param>
        /// <param name="RealName"></param>
        /// <param name="WarehouseId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string Number, [FromQuery] string WareHouseId, [FromQuery] string RealName, [FromQuery] string Status)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                Number = Number ?? "";
                RealName = RealName ?? "";
                WareHouseId = WareHouseId ?? "";
                Status = Status ?? "";

                var list = await Services.GetAsync(PageIndex, PageSize, Number, WareHouseId, Status, RealName);
                status.Data = list;
                status.Count = await Services.GetCountAsync(Number, WareHouseId, Status, RealName);

            }
            catch (Exception ex)
            {

                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他出库-分页获取", ex, HttpContext);

            }
            return Ok(status);

           
        }

        /// <summary>
        /// 其他出库新增
        /// </summary>
        /// <param name="model"></param>
        /// <param name="OtherExWareHouseItemString"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task <IActionResult> Add([FromForm] OtherExWareHouse model,[FromForm] string OtherExWareHouseItemString)
        {
            var status = BaseStatus.CreateInstance();
            List<OtherExWareHouseItem> list = new List<OtherExWareHouseItem>();
            try
            {
                if (!string.IsNullOrEmpty(OtherExWareHouseItemString))
                {
                    list = JsonConvert.DeserializeObject<List<OtherExWareHouseItem>>(OtherExWareHouseItemString);
                    list.ForEach(x =>
                    {

                        x.OtherExWareHouseId = model.Id;
                    });
                }
                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;
                var isSuccess = await Services.AddAsync(model, list);
                if (isSuccess)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {

                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他出库-新增", ex, HttpContext);

            }
            return Ok(status);
        }
        /// <summary>
        /// 其他出库修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="OtherExWareHouseItemString"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] OtherExWareHouse model, [FromForm] string OtherExWareHouseItemString)
        {
            var status = BaseStatus.CreateInstance();
            List<OtherExWareHouseItem> list = new List<OtherExWareHouseItem>();
            try
            {
                if (!string.IsNullOrEmpty(OtherExWareHouseItemString))
                {
                    list = JsonConvert.DeserializeObject<List<OtherExWareHouseItem>>(OtherExWareHouseItemString);
                    list.ForEach(x =>
                    {
                        x.OtherExWareHouseId = model.Id;
                    });
                }
                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;
                var isSuccess = await Services.UpdateAsync(model, list);
                if (isSuccess)
                {
                    status.Msg = "修改成功";
                }
                   
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他出库-修改", ex, HttpContext);
            }

            return Ok(status);
        }
        /// <summary>
        /// 其他出库批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.Delete(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他出库-批量删除", ex, HttpContext);
            }

            return Ok(status);
        }
        /// <summary>
        /// 其他出库删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del ([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.Delete(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他出库-删除", ex, HttpContext);
            }

            return Ok(status);
        }
        /// <summary>
        /// 获取商品
        /// </summary>
        /// <param name="OtherExWareHouseId"></param>
        /// <param name="WareHouseId"></param>
        /// <returns></returns>
        [HttpGet("GetOtherExWareHousebyAsync")]

        public async Task<IActionResult> GetOtherExWareHousebyAsync([FromQuery] string OtherExWareHouseId, [FromQuery] string WareHouseId)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await Services.GetOtherExWareHouseItembyAsync(OtherExWareHouseId, WareHouseId);
                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他入库-获取商品", ex, HttpContext);
            }

            return Ok(status);
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        [HttpPut ("UpdateStatus")]
        public async Task<IActionResult> UpdateStatus ([FromForm] long Id,[FromForm] int Status)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                // 根据 Id 查询数据库，找到对应的对象
                var isSuccess = await Services.UpdateStatus(Id, Status);


                if (isSuccess)
                {
                    var msg = Status == 0 ? "反审成功" : "审核成功";
                    status.Code = 0;
                    status.Msg = msg;
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("其他入库-修改状态", ex, HttpContext);
            }

            return Ok(status);
        }
    }
}
